package util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.StringUtils;
import pojo.ConfigPoJo;
import pojo.PostResult;
import pojo.ServiceDesc;
import pojo.postman.PostManChildItem;
import pojo.postman.PostManFirstInfo;
import pojo.postman.PostManPojo;
import pojo.postman.PostManRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class PostManUtil {

    private static ConfigPoJo configPoJo;

    private static String key = "参数库V2";

    static {
        try {
            configPoJo = ConfigUtil.getConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private final static ThreadLocal<PostManPojo> postManPojoThreadLocal = ThreadLocal.withInitial(() ->
            new PostManPojo(
                    new PostManFirstInfo(UUID.randomUUID().toString(),
                            key,
                            "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"))
    );

    public static File writerFile() throws Exception {
        File dir = new File(configPoJo.getLogSavePath() + "postMan");
        File file = new File(configPoJo.getLogSavePath() + "postMan/" + "postman.txt");
        if (!dir.exists()) {
            file.mkdirs();
        }
        PrintWriter printWriter = new PrintWriter(new FileOutputStream(file));
        printWriter.println(JSONObject.toJSONString(postManPojoThreadLocal.get()));
        printWriter.close();
        return file;
    }

    public static void addPostManItem(ServiceDesc desc, PostResult result) {
        PostManChildItem postManChildItem = new PostManChildItem();
        postManChildItem.setName(desc.getServiceCode() + "-" + desc.getServiceDesc() + "-" + result.getResultType());

        PostManRequest postManRequest = new PostManRequest();
        postManRequest.setMethod("POST");
        postManRequest.setHeader(new ArrayList<>());
        postManRequest.setBody(new PostManRequest.BodyDTO(
                "raw", JSONFormatUtil.formatJSON(result.getRequest()), new PostManRequest.BodyDTO.OptionsDTO(new PostManRequest.BodyDTO.OptionsDTO.RawDTO("json"))
        ));

        String uri = desc.getFirstUrl();
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        String ip = configPoJo.getSysIp().substring(0, configPoJo.getSysIp().indexOf(":"));
        String port = configPoJo.getSysIp().substring(configPoJo.getSysIp().indexOf(":") + 1);
        List<String> hostList = Lists.newArrayList();
        hostList.add(ip);

        List<String> path1 = Arrays.asList(uri.split("/").clone());
        List<String> path = new ArrayList<>();
        for (String s : path1) {
            if (!StringUtils.isEmpty(s)) {
                path.add(s);
            }
        }

        postManRequest.setUrl(new PostManRequest.UrlDTO(result.getUrl(), hostList, port, path));
        postManChildItem.setRequest(postManRequest);

        postManPojoThreadLocal.get().getFirstByKey(key).getItem().add(postManChildItem);
    }

}
